package ATM.logic;

import java.util.ArrayList;
import java.util.Timer;

import javax.swing.JOptionPane;
import javax.swing.RepaintManager;

import ATM.data.Accounts;
import ATM.data.AtmData;
import ATM.data.CustomData;
import ATM.gui.Screen;

public class AcLMethods {

	Screen screen;
	AtmData atmData;
	CustomData customData;
	ArrayList<Accounts> accounts;

	String un;
	String pw;
	StringBuilder usb = new StringBuilder();
	StringBuilder psb = new StringBuilder();
	private int counter = 0;

	public AcLMethods(Screen screen) {
		this.screen = screen;
		atmData = new AtmData(screen);
		customData = new CustomData(screen);

	}

	public void login() {
		if (screen.getWelcome().getPasswordField().getText().isEmpty()) {
			JOptionPane.showMessageDialog(screen.getWelcome(),
					"enter text in textfield");
		} else {
			un = screen.getWelcome().getUserField().getText();
			pw = screen.getWelcome().getPasswordField().getText();
			accounts = customData.getCustomersAccounts(Integer.parseInt(un));

			if (atmData.Authenticated(un, pw) == false) {
				screen.getWelcome().getLblWrongPassword()
						.setText("Wrong password");
				screen.getWelcome().getUserField().setText(null);
				screen.getWelcome().getPasswordField().setText(null);
				usb.delete(0, 10);
			}
			if (atmData.Authenticated(un, pw) == true) {
				String identification = atmData.Identification(un);
				// usb.delete(0, 20);
				if (identification.equals("Admin")) {
					screen.show("MENUADMIN");
				} else {
					screen.show("MENUCUST");
					String avBal = Double.toString(accounts.get(0)
							.getAvailableBalance());
					screen.getMenuCust().getLblBalance()
							.setText("Available balance: " + avBal);
				}
			}
		}
	}

	public void ViewCurrentBalance(ArrayList<Accounts> accounts) {
		String accType = accounts.get(counter).getType();
		String allaccounts = Integer.toString(accounts.size());
		String avBal = Double.toString(accounts.get(counter)
				.getAvailableBalance());
		String toBal = Double.toString(accounts.get(counter).getTotalBalance());
		String rate = Double.toString(accounts.get(counter).getInterestRate());
		String ovDraft = Double.toString(accounts.get(counter).getOverdraft());
		screen.getViewBalance().setTextField(accType, avBal, toBal, rate,
				ovDraft, Integer.toString(counter + 1), allaccounts);

	}

	public void Withdraw() {
		screen.show("WITHDRAWCASH");
		int avBal = (int) accounts.get(0).getAvailableBalance();

		int oDraft = (int) accounts.get(0).getOverdraft();
		System.out.println("a"+avBal+" ovd"+oDraft);
		avBal += oDraft;
		
		screen.getWithdraw().getLblBalance()
				.setText("Available balance: " + avBal);

		if (avBal < 100) {
			screen.getWithdraw().getWd100().setEnabled(false);
		}
		if (avBal < 200) {
			screen.getWithdraw().getWd200().setEnabled(false);
		}
		if (avBal < 500) {
			screen.getWithdraw().getWd500().setEnabled(false);
		}
		if (avBal < 1000) {
			screen.getWithdraw().getWd1000().setEnabled(false);
		}
	}

	public void WithdrawAmount(int amount) {
		customData.WithdrawCash(amount, customData.getAccount(un));
		TempMessage("Withdrawing "+amount+" kr.");
	}

	public void Deposit() {
		int accountID = customData.getAccount(un);
		int amount = 0;
		
		if (screen.getDeposit().getTextFieldDeposit().getText().equals("") != true) {
			amount = Integer.parseInt(screen.getDeposit().getTextFieldDeposit()
					.getText());
		}if (amount > 0) {
			customData.DepositAmount(amount, accountID);
			TempMessage(amount +" kr. deposit");
		}else{
			TempMessage("Not deposit");
		}
		
	}

	public void Exit() {
		screen.getWelcome().getUserField().setText(null);
		screen.getWelcome().getPasswordField().setText(null);
		screen.getWelcome().getLblWrongPassword().setText(null);
		screen.getDeposit().getTextFieldDeposit().setText(null);

		usb.delete(0, 20);
		psb.delete(0, 20);
		screen.show("WELCOME");

	}

	public void TempMessage(String s) {
		screen.getTempScreen().getSetlabel().setText(s);
		screen.show("TEMPSCREEN");
		TimerTempScreen t2 = new TimerTempScreen(screen);
		t2.start();
	}

	public void Numpad(String ac) {
		for (int i = 0; i <= 9; i++) {
			String np = "b" + i;
			String dnp = "d" + i;
			if (ac.equals(np)) {
				String nbd = Integer.toString(i);
				ArrayList<String> list = new ArrayList<String>();
				list.add(nbd);

				for (String s : list) {
					usb.append(s);
					psb.append(s);
				}

				if (usb.toString().length() <= 5) {
					screen.getWelcome().getUserField().setText(usb.toString());
					psb.delete(0, 20);
				}

				if (psb.toString().length() <= 5) {
					screen.getWelcome().getPasswordField()
							.setText(psb.toString());
				}

			}

			else if (ac.equals(dnp)) {
				String nbd = Integer.toString(i);
				ArrayList<String> list = new ArrayList<String>();
				list.add(nbd);

				for (String s : list) {
					usb.append(s);
				}
				screen.getDeposit().getTextFieldDeposit()
						.setText(usb.toString());

			}
		}
	}

}
